﻿using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using S.PurchaseAndSales.Domain.Dto;
using S.PurchaseAndSales.Domain.Model;
using S.PurchaseAndSales.Domain.SqlServer;
using S.PurchaseAndSales.Infrastructure.Interface.PurchaseForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.PurchaseAndSales.Infrastructure.Impl.PurchaseForm
{
    /// <summary>
    /// 供应商
    /// </summary>
    public class VendorRepository : BaseRepository<VendorModel>, IVendorRepository
    {
        private readonly IConfiguration configuration;

        public VendorRepository(SqlDbContext db, IConfiguration configuration) : base(db)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 供应商分页显示查询
        /// </summary>
        /// <param name="venDorCode">编号</param>
        /// <param name="venDorName">名称</param>
        /// <param name="responsiblePhone">负责人电话</param>
        /// <param name="markets">所属市场</param>
        /// <param name="salesman">服务人员/业务员</param>
        /// <param name="useState">使用状态</param>
        /// <returns></returns>
       public async Task<IQueryable<GetVendorPageDto>> GetVendorPage()
        {
            //List<VendorModel> list = db.VendorModels.Where(x => (venDorCode == 0 || x.VenDorCode == venDorCode) && (string.IsNullOrEmpty(venDorName) || x.VendorName.Contains(venDorName)) && (string.IsNullOrEmpty(responsiblePhone) || x.ResponsiblePhone.Contains(responsiblePhone)) && (string.IsNullOrEmpty(markets) || x.Markets.Contains(markets)) && (string.IsNullOrEmpty(salesman) || x.Salesman.Contains(salesman)) && (useState == null || x.UseState == useState) && x.IsDel==false).ToList();

            string connectionString = configuration.GetValue<string>("ConnectionStrings");

            using (var connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();

                    StringBuilder sqlQuery = new StringBuilder();
                    sqlQuery.Append(@"
                        select v.VendorSnowId,v.VenDorCode,v.VendorType,v.ResponsiblePhone,v.VendorName,
                        b.BusinessName,v.UseState,v.UnitsOfReference,v.Markets,v.Salesman,v.UpdateBy,v.UpdateTime,v.IsDel
                        from  dbo.VendorModels as v
                        join dbo.BusinessModel as b
                        on v.BusinessId = b.BusinessId
                        where v.IsDel=0");
                  
                    var result = connection.Query<GetVendorPageDto>(sqlQuery.ToString()).AsQueryable();
                    
                    return await Task.FromResult(result);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
}
